package com.et114.modules.system.dictionary.tag;

import com.et114.core.utility.GetterUtil;
import com.et114.core.utility.Validator;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.beanutils.PropertyUtils;

public class DictOptionTag extends TagSupport {
	private static final long	serialVersionUID	= 1973900272506218844L;
	protected String			biztypekey;							
	protected String			order;													
	protected String			name;
	protected String 			property;
	protected String			selected;							
	protected String			emptyOption;							
	protected String			status;									
	protected String			match;
//	private MessageResources	messages;
	private String				compareValue;
	private boolean				needCompare;
	
	public DictOptionTag ( ) {
		needCompare = false;
	}
	
	public String getCompareValue ( ) {
		return compareValue;
	}
	
	public void setCompareValue ( String compareValue ) {
		this.compareValue = compareValue;
	}
	
	public int doStartTag ( ) throws JspException {
		/*
		HttpServletRequest request = ( HttpServletRequest ) pageContext
				.getRequest ( );
		messages = ( MessageResources ) request
				.getAttribute ( "org.apache.struts.action.MESSAGE" );
				*/
		if ( Validator.isNull ( selected ) || Validator.isNotNull ( name ) )
			calculateMatchValues ( );
		return 6;
	}
	
	private void calculateMatchValues ( ) throws JspException {
		Object bean  = pageContext.findAttribute(name);
		try {
			match =  PropertyUtils.getProperty(bean, property)+"";
		} catch ( IllegalAccessException e ) {
			e.printStackTrace ( );
		} catch ( InvocationTargetException e ) {
			e.printStackTrace ( );
		} catch ( NoSuchMethodException e ) {
			e.printStackTrace ( );
		}
	}
	
	private boolean isMatched ( String value ) {
		if ( value == null )
			return false;
		if ( value.equals ( selected ) )
			return true;
		if ( Validator.isNull ( selected ) ) {
			return value.equals ( match );
		}
		return false;
	}
	
	public String getEmptyOption ( ) {
		return emptyOption;
	}
	
	public String getOrder ( ) {
		return order;
	}
	
	public String getStatus ( ) {
		return status;
	}
	
	public void setEmptyOption ( String string ) {
		emptyOption = string;
	}
	
	public void setOrder ( String string ) {
		order = string;
	}
	
	public void setStatus ( String string ) {
		status = string;
	}
	
	public String getProperty ( ) {
		return property;
	}

	public void setProperty ( String property ) {
		this.property = property;
	}

	public int doEndTag ( ) throws JspException {
//		HttpServletRequest request = ( HttpServletRequest ) pageContext
//				.getRequest ( );
		DictionaryCache cache = DictionaryCache.getInstance ( );
		StringBuffer results = new StringBuffer ( );
		List options = null;
		if ( "all".equals ( status ) )
			try {
				options = cache.listBizOptions ( biztypekey );
			} catch ( Exception e1 ) {
				// TODO Auto-generated catch block
				e1.printStackTrace ( );
			}
		else
			try {
				options = cache.listBizOptions ( biztypekey , GetterUtil
						.getBoolean ( status ) );
			} catch ( Exception e1 ) {
				// TODO Auto-generated catch block
				e1.printStackTrace ( );
			}
		if ( GetterUtil.getBoolean ( "order" ) )
			Collections.sort ( options , new BizOptionComparator ( ) );
		if ( Validator.isNotNull ( compareValue ) )
			needCompare = true;
		boolean goon = true;
		for ( Iterator it = options.iterator ( ) ; it.hasNext ( ) ; ) {
			BizOption option = ( BizOption ) it.next ( );
			String optionvalue = option.getOptionValue ( );
			if ( needCompare ) {
				goon = false;
				for ( StringTokenizer tokenizer = new StringTokenizer (
						compareValue , "," ) ; tokenizer.hasMoreTokens ( ) ; ) {
					String value = tokenizer.nextToken ( );
					if ( value.equals ( optionvalue ) )
						goon = true;
				}
				
			}
			if ( goon ) {
				results.append ( "<option value=\"" );
				results.append ( optionvalue );
				results.append ( "\"" );
				if ( isMatched ( optionvalue ) )
					results.append ( " selected=\"selected\"" );
				results.append ( ">" );
				results.append ( option.getOptionName ( ) );
				results.append ( "</option>" );
			}
		}
		
		JspWriter writer = pageContext.getOut ( );
		try {
			writer.write ( results.toString ( ) );
		} catch ( IOException e ) {
			throw new JspException ( e.getMessage ( ) );
		}
		return 6;
	}
	
	public String getBiztypekey ( ) {
		return biztypekey;
	}
	
	public void setBiztypekey ( String biztypekey ) {
		this.biztypekey = biztypekey;
	}
	
	public String getName ( ) {
		return name;
	}
	
	public void setName ( String name ) {
		this.name = name;
	}
	
	public String getSelected ( ) {
		return selected;
	}
	
	public void setSelected ( String selected ) {
		this.selected = selected;
	}
}
